<template>
  <h3>图书类别管理</h3>
  <el-divider />
  <div class="opt-div">
    <div>
      <el-form v-model="cate" :inline="true" class="demo-form-inline">
        <el-form-item label="类别名称">
          <el-input v-model="cate.cname" clearable placeholder="请输入"
        /></el-form-item>
        <el-form-item label="状态">
          <el-select v-model="cate.state" placeholder="全部" clearable>
            <el-option label="全部" value="" />
            <el-option label="正常" :value="0" />
            <el-option label="删除" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item >
        <el-button @click="category_search" type="primary">搜索</el-button>
        <el-button type="primary" @click="addDialogFormVisible = true"
          ><el-icon><Plus /></el-icon>添加图书类别</el-button></el-form-item>
        </el-form>
    </div>
    <el-pagination
      v-model:current-page="pages.pageNum"
      v-model:page-size="pages.pageSize"
      :page-sizes="[10, 20, 50]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="categoryData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <el-table :data="categoryData.list">
    <el-table-column width="100px" label="类别编号" prop="categoryId" />
    <el-table-column prop="cname" label="类别名称" />

    <!-- <el-table-column width="100px" label="状态" >
      <template #default="scope">
        <el-switch v-model="scope.row.cstate" :active-value="0" :inactive-value="1"/>
      </template>
    </el-table-column> -->

    <el-table-column label="类别状态" width="180px">
      <template #default="scope">
        <el-text class="mx-1" v-if="scope.row.cstate != 0" type="danger"
          >删除</el-text
        >
        <el-text v-else class="mx-1" type="success">正常</el-text>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="180px" />
    <el-table-column prop="updateTime" label="更新时间" width="180px" />
    <el-table-column label="操作" width="180px">
      <template #default="scope">
        <el-button
          type="warning"
          @click="category_update(scope.row)"
          size="small"
          >编辑</el-button
        >
        <el-button type="danger" v-if="scope.row.cstate ==0 " @click="category_del(scope.row)" size="small"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="addDialogFormVisible" title="添加图书类别" width="500">
    <el-form :model="category">
      <el-form-item label="类别名称">
        <el-input v-model="category.cname" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="category_add"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="updateDialogFormVisible" title="更新图书类别" width="500">
    <el-form :model="category">
      <el-form-item label="类别名称">
        <el-input v-model="category.cname" />
      </el-form-item>
      <el-form-item label="类别状态">
        <el-radio-group v-model="category.cstate">
          <el-radio label="正常" :value="0" />
          <el-radio label="删除" :value="1" />
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="cate_update_ok"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import http from "../axios";

import { onMounted, ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";

const categoryData = ref({});
const addDialogFormVisible = ref(false);
const updateDialogFormVisible = ref(false);

const temp_obj = ref({
  
  0:'正常',
  1:'删除',
})

const category = ref({});
const cate = ref({
  cname: "",
  state: 0,
});
const pages = ref({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  category_list();
});

const category_list = () => {
  http
    .get("/category/list", {
      params: { ...pages.value, ...cate.value },
    })
    .then((resp) => {
      console.log("-----------", resp);
      categoryData.value = resp.data.data;
    });
};

const category_del = (category) => {
  ElMessageBox.confirm(
    `您确认删除《${category.cname}》 类别信息吗？`,
    "删除提示"
  ).then(() => {
    http.delete(`/category/del/${category.categoryId}`).then((resp) => {
      if (resp.data.code != "200") {
        ElMessage.error(resp.data.message);
        return;
      }
      ElMessage.success(resp.data.message);
      category_list();
    });
  });
};

const category_add = () => {
  http.post(`/category/add`, category.value).then((resp) => {
    if (resp.data.code != "200") {
      ElMessage.error(resp.data.message);
      return;
    }
    addDialogFormVisible.value = false;
    ElMessage.success(resp.data.message);
    category_list();
    category.value = {};
  });
};

const category_update = (cate) => {
  category.value = JSON.parse(JSON.stringify(cate));
  console.log(category.value);
  updateDialogFormVisible.value = true;
};

const cate_update_ok = () => {
  http.put("/category/update", category.value).then((resp) => {
    if (resp.data.code != "200") {
      ElMessage.error(resp.data.message);
      return;
    }
    updateDialogFormVisible.value = false;
    ElMessage.success(resp.data.message);
    category_list();
    category.value = {};
  });
};

const handleCurrentChange = (val) => {
  pages.value.pageNum = val;
  category_list();
};

const handleSizeChange = (val) => {
  pages.value.pageSize = val;
  category_list();
};

const category_search = () => {
  console.log(cate.value);
  category_list();
};
</script>

<style>
.opt-div {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  align-items: center !important;
}

.demo-form-inline .el-input {
  --el-input-width: 200px;
}

.demo-form-inline .el-select {
  --el-select-width:120px;
}
.demo-form-inline{

  padding-bottom: 0 !important;
}
.el-form-item{
  margin-bottom: 0;
}
</style>
